package com.shishuo.cms.action;

import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.shishuo.cms.entity.Article;
import com.shishuo.cms.entity.vo.FolderVo;

/**
 * 此类提供rest服务，用于第三方接口调用
 * 
 * @author youyangwang
 * 
 */
@Controller
@RequestMapping({ "/rs" })
public class ApiAction extends BaseAction {
	@RequestMapping(value = "/apiAddArticle", method = RequestMethod.POST)
	public String apiAddArticle(
			@RequestParam(value = "articleTitle", defaultValue = "") String title,
			@RequestParam(value = "articleContent", defaultValue = "") String content,
			@RequestParam(value = "stockCode", defaultValue = "") String stockCode,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html;charset=utf-8");
			long folderId = 0;
			if (!"".equals(stockCode)) {
				FolderVo folderVo = folderService
						.getFirstFolderByName(stockCode);
				if (null != folderVo) {
					folderId = folderVo.getFolderId();
				}
			}
			if (folderId>0) {
				Article article = new Article();
				article.setContent(content);
				article.setTitle(title);
				fileService.addArticleByArticle(folderId, article);
				response.getWriter().println("success");
			}else {
				response.getWriter().println("error:not found folderId");
			}
			response.flushBuffer();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
}
